package com.fuqi.android.zhongcai.base.view.activity;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.request.api.SanBaoListApi;
import com.fuqi.android.zhongcai.base.request.result.SanBaoResult;
import com.fuqi.android.zhongcai.base.view.adapter.BaseRecyclerViewAdapter;
import com.fuqi.android.zhongcai.base.view.adapter.SanBaoAdapter;
import com.fuqi.android.zhongcai.base.view.widget.CustomProgress;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.android.zhongcai.utils.LogUtils;
import com.fuqi.framework.gson.GsonHelper;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

import butterknife.Bind;

public class SanBaoActivity extends BaseActivity {


    @Bind(R.id.recyclerview)
    XRecyclerView mRecyclerView;
    private ArrayList<SanBaoResult> listData;
    private SanBaoAdapter sanBaoAdapter;


    @Override
    public void initView() {
        listData = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        View header = LayoutInflater.from(this).inflate(R.layout.recyclerview_header, (ViewGroup)findViewById(android.R.id.content),false);
        mRecyclerView.addHeaderView(header);
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(
//                this, DividerItemDecoration.VERTICAL_LIST));
        sanBaoAdapter = new SanBaoAdapter(this,listData);
        mRecyclerView.setAdapter(sanBaoAdapter);
    }


    @Override
    public void initListener() {
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPage=1;
                CustomProgress.show(SanBaoActivity.this, "正在处理...",true,null);
                RequestManager.getInstance().call(new SanBaoListApi(new SanBaoListApi.SanBaoListParams
                        (String.valueOf(showCount),String.valueOf(currentPage)),
                        respSanBaoListener, errorListener));
            }

            @Override
            public void onLoadMore() {
                currentPage ++;
                CustomProgress.show(SanBaoActivity.this, "正在处理...",true,null);
                RequestManager.getInstance().call(new SanBaoListApi(new SanBaoListApi.SanBaoListParams
                        (String.valueOf(showCount),String.valueOf(currentPage)),
                        respSanBaoListener, errorListener));
            }
        });
        sanBaoAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener<SanBaoResult>() {
            @Override
            public void onItemClick(View view, int position, SanBaoResult data) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("data",data);
                ActivityUtils.startActivity(SanBaoActivity.this,SanBaoDetailActivity.class,bundle);

            }
        });


    }

    @Override
    public void initData() {

        titleBar.setTitle("中财三保");
        RequestManager.getInstance().call(new SanBaoListApi(new SanBaoListApi.SanBaoListParams
                (String.valueOf(showCount),String.valueOf(currentPage)),
                respSanBaoListener, errorListener));

    }

    Response.Listener<SanBaoResult[]> respSanBaoListener = new Response.Listener<SanBaoResult[]>() {

        @Override
        public void onResponse(SanBaoResult[] response) {

            CustomProgress.close();
            if (HandleCode.requestSuccess()) {
                if (response.length == 0)
                    mRecyclerView.noMoreLoading();
                if (currentPage == 1 && listData != null)
                    listData.clear();
                for (int i = 0; i < response.length; i++) {
                    listData.add(response[i]);
                }

                sanBaoAdapter.notifyDataSetChanged();
            }
            LogUtils.i("response OrderListResult: " + GsonHelper.writeValue(response));
            mRecyclerView.refreshComplete();
            mRecyclerView.loadMoreComplete();
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());

        }
    };


    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_san_bao;
    }
}
